﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using System.Text;

namespace HY.WEB.SA
{
    public partial class school_view_edu : PageBase
    {
        public string us,ubh, whe = "", path = "";
        public string tab = "sch_edu";
        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            ubh = Session["bh"].ToString();

            if (!IsPostBack)
            {
                check_delbtn();

                if (!String.IsNullOrEmpty(Request["sid"]))
                {
                    hidsid.Text = Request["sid"].Trim();
                }
                if (!String.IsNullOrEmpty(Request["schbh"]))
                {
                    hidsbh.Text = Request["schbh"].Trim();
                    hidsid.Text = BLL.Bllbase.get_val("school", "sid", "schbh='" + hidsbh.Text + "'");
                    whe="schbh='" + hidsbh.Text + "'";
                }

                btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");
                dtteldel.OnClientClick = Grid2.GetNoSelectionAlertReference("至少选择一项！");
                btaddedu.OnClientClick = Window1.GetShowReference("../sales/schedu_add.aspx?schbh=" + hidsbh.Text + "", "添加信息");
                btaddtel.OnClientClick = Window2.GetShowReference("../sales/tel_add.aspx?cbh=" + hidsbh.Text + "", "添加电话记录");
      
                fill2();
                BindGrid();
            }

        
        }

        //允许本人及管理员删除电话记录
        void check_delbtn()
        {
            bool btnv = BLL.Bllbase.check_exits("comp_tel", "pbh='" + ubh + "'");
            bool btnr = BLL.Bllbase.check_exits("user_safe", "ubh='" + ubh + "' and menu='1'");
        
            if (btnv || btnr)
                dtteldel.Visible = true;
            else
                dtteldel.Visible = false;

        }


        private void BindGrid()
        {

            //职位表
            DataTable table = BLL.Bllbase.get_table("sch_edu", "schbh='" + hidsbh.Text + "'");
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", "sid", "desc");

            Grid1.DataSource = view1;
            Grid1.DataBind();

            //电话记录表
            DataTable table2 = BLL.Bllbase.get_table("comp_tel", "cbh='" + hidsbh.Text + "'");
            DataView view2 = table2.DefaultView;
            view2.Sort = String.Format("{0} {1}", "pdtime", "desc");

            Grid2.DataSource = view2;
            Grid2.DataBind();

        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            // Alert.ShowInTop("弹出窗口关闭了！");
            //string key = txt.Text;
            //stype = hidstype.Text;
            //try
            //{
            //    if (key == "" & stype != "")
            //        whe = whe + " and " + BLL.pub.pubwhere.get_where_sql(us, tab);

            //    BindGrid();


            //}
            //catch (Exception ex)
            //{
            //    // Alert.Show("查询条件错误");
            //    whe = " cbh in (select cbh from company_fav where sbh='" + bh + "')";
            //}
            //finally
            //{
            //    whe = " cbh in (select cbh from company_fav where sbh='" + bh + "')";
            //}

            //BindGrid();
            BindGrid();
        }
        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
        }



        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = BLL.Bllbase.get_table(tab, whe);

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid1.DataSource = view1;
            Grid1.DataBind();
        }


        //电话记录翻页
        protected void Grid2_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid2.PageIndex = e.NewPageIndex;
        }

        //电话记录排序
        protected void Grid2_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = BLL.Bllbase.get_table("comp_tel", "cbh='" + hidsbh.Text + "'");

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid2.DataSource = view1;
            Grid2.DataBind();
        }

        void fill2()
        {
            ///BLL.SC.schools S = new BLL.SC.schools();

            DataTable d =BLL.Bllbase.get_table("school","sid=" + hidsid.Text);
            hidsid.Text = d.Rows[0]["sid"].ToString().Trim();

            lblbh.Text = d.Rows[0]["schbh"].ToString().Trim();
            txtschool.Text = d.Rows[0]["schname"].ToString().Trim();

            txtperson.Text = d.Rows[0]["person"].ToString().Trim();

            txttel.Text = d.Rows[0]["stel"].ToString().Trim();

            txtfax.Text = d.Rows[0]["sfax"].ToString().Trim();
            txtemail.Text = d.Rows[0]["semail"].ToString().Trim();

            txtaddress.Text = d.Rows[0]["saddress"].ToString().Trim();
            txtpost.Text = d.Rows[0]["spost"].ToString().Trim();

            txtwebsite.Text = d.Rows[0]["sweb"].ToString().Trim();
            txtqq.Text = d.Rows[0]["qq"].ToString().Trim();
            txtmsn.Text = d.Rows[0]["msn"].ToString().Trim();

            updtime.Text =DateTime.Parse(d.Rows[0]["updtime"].ToString()).ToString("yyyy-MM-hh");
            lbldtime.Text = DateTime.Parse(d.Rows[0]["rdate"].ToString()).ToString("yyyy-MM-hh");

            lblsales.Text = d.Rows[0]["sales"].ToString().Trim();

            lblsx.Text = d.Rows[0]["sxdate"].ToString().Trim();
            lbljob.Text = d.Rows[0]["pjob"].ToString().Trim();
            lblsex.Text = d.Rows[0]["psex"].ToString().Trim();
            lblsize.Text = d.Rows[0]["psize"].ToString().Trim();

            lbltype.Text = d.Rows[0]["schtype"].ToString().Trim();
            lblxz.Text = d.Rows[0]["schxz"].ToString().Trim();

            lblmemo.Text = Server.HtmlDecode(d.Rows[0]["memo"].ToString().Trim());
            txttelm.Text = d.Rows[0]["mtel"].ToString().Trim();

            }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int val = Grid1.PageIndex * Grid1.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
                sb.Append(Grid1.DataKeys[row + val][0].ToString());
                sb.Append(",");
            }

            // Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));
            HY.BLL.Bllbase.DeleteList("sch_edu", sb.ToString().TrimEnd(','));
            BindGrid();
        }

        protected void btnDelete2_Click(object sender, EventArgs e)
        {

            int val = Grid2.PageIndex * Grid2.PageSize;

            StringBuilder sb = new StringBuilder();

            foreach (int row in Grid2.SelectedRowIndexArray)
            {
                sb.Append(Grid2.DataKeys[row + val][0].ToString());
                sb.Append(",");
            }

            // Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));
            HY.BLL.Bllbase.DeleteList("comp_tel", sb.ToString().TrimEnd(','));
            BindGrid();

        }

       
    }
}